import Vue from 'vue'
const defaultTag = 'Other'

const state = {
  sentence: 'play the song uptown funk',
  tags: [
    'Other',
    'MediaType',
    'SongName.fst'
  ],
  output: [
    defaultTag,
    defaultTag,
    defaultTag,
    defaultTag,
    defaultTag
  ]
}

const getters = {
  words: state => {
    const normalizedSentence = state.sentence.replace(/ +/g, ' ')
    return normalizedSentence.split(' ')
  },
  wordsGrm: state => {
    const words = state.sentence.replace(/ +/g, ' ').split(' ')
    return state.output.reduce((result, current, index) => {
      if (index > 0 && current.endsWith('.fst') && state.output[index - 1].endsWith('.fst')) {
        return result
      }
      if (current.endsWith('.fst')) {
        result.push(`eps : "[${current}]"`)
      } else {
        result.push(`"[${words[index]}]" : "[${current}]"`)
      }
      return result
    }, []).join(' ')
  }
}

const mutations = {
  setSentence (state, sentence) {
    state.sentence = sentence
  },
  addTag (state, tag) {
    state.tags.push(tag)
  },
  removeTag (state, tag) {
    state.tags.splice(state.tags.indexOf(tag), 1)
  },
  setOutputByIndex (state, payload) {
    Vue.set(state.output, payload.index, payload.output)
  }
}

export default {state, getters, mutations}
